package com.lc1993929.shucao.controller;

import com.alibaba.fastjson.JSON;
import com.lc1993929.shucao.constants.Constants;
import com.lc1993929.shucao.entity.ClientRes;
import com.lc1993929.shucao.entity.PlayerMap;
import com.lc1993929.shucao.service.HeiMaService;
import com.lc1993929.shucao.util.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/player")
@Slf4j
public class Controller {

    @Autowired
    private HeiMaService heiMaService;

    @PostMapping("/start")
    public void start(@RequestBody PlayerMap playerMap) {
        Constants.playerMap = playerMap;
        heiMaService.start(playerMap);
    }

    @PostMapping("/move")
    public ClientRes move(@RequestBody PlayerMap playerMap) {
        try {
            //long start = System.currentTimeMillis();

            //log.info(JSON.toJSONString(playerMap));
            //MapUtil.mapPrint(playerMap.getMap());


            Constants.playerMap = playerMap;
            ClientRes move = heiMaService.move(playerMap);
/*            long end = System.currentTimeMillis();
            if (end - start >= 190) {
                log.info(JSON.toJSONString(playerMap));
                MapUtil.mapPrint(playerMap.getMap());
            }*/
            return move;
        } catch (Throwable e) {
            e.printStackTrace();
            return ClientRes.builder()
                    .player1(MapUtil.getRandomDirection())
                    .player2(MapUtil.getRandomDirection())
                    .player3(MapUtil.getRandomDirection())
                    .player4(MapUtil.getRandomDirection())
                    .build();
        }
    }


}
